package com.yami.shop.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.enums.AirdropRecordEnum;
import com.yami.shop.bean.event.AirDropSuccessEvent;
import com.yami.shop.bean.model.BusinessNft;
import com.yami.shop.bean.model.ShopDetail;
import com.yami.shop.bean.param.SendUserDropsParam;
import com.yami.shop.bean.param.UserUpdateParam;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.dao.BusinessNftMapper;
import com.yami.shop.service.BusinessNftService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 取币业务记录表 服务实现类
 * </p>
 *
 * @author author
 * @since 2023-05-18
 */
@Service
@RequiredArgsConstructor
public class BusinessNftServiceImpl extends ServiceImpl<BusinessNftMapper, BusinessNft> implements BusinessNftService {
    @Autowired
    private BusinessNftMapper businessNftMapper;


    @Override
    public BusinessNft getInfoLast() {
        return businessNftMapper.getInfoLast();
    }

    @Override
    public void updateStatus( Long businessNftId,Integer status, String reason) {
        businessNftMapper.updateStatus(businessNftId,status, reason);
    }

    @Override
    public void updateStatusBatchById(List<Long> ids, Integer status) {
        businessNftMapper.updateStatusBatchById(ids, status);
    }

    private final ApplicationEventPublisher eventPublisher;

    @Override
    public void bindDropById(UserUpdateParam param, ShopDetail shopDetail) {
        Map<Integer,List < SendUserDropsParam >> collect = param.getDrops().stream().collect(Collectors.groupingBy(SendUserDropsParam :: getType));
        collect.entrySet().forEach(entry -> {
            if(entry.getKey() == AirdropRecordEnum.PROD.value() || entry.getKey() == AirdropRecordEnum.PROD_PRIORITY.value()){
                // 如果是空投商品或商品类权益（优先购、实物兑换）
                eventPublisher.publishEvent(new AirDropSuccessEvent(param.getUserIds(), entry.getValue(), param.getBusinessType(), shopDetail.getShopId(),entry.getKey(),param.getAirdropBusinessId(),param.getMainProdId()));
            }
            if(entry.getKey() == AirdropRecordEnum.BOX.value() || entry.getKey() == AirdropRecordEnum.BOX_PRIORITY.value()){
                // 如果是空投盲盒或盲盒类权益（优先购、实物兑换）
                eventPublisher.publishEvent(new AirDropSuccessEvent(param.getUserIds(), entry.getValue(), param.getBusinessType(), shopDetail.getShopId(),entry.getKey(),param.getAirdropBusinessId(),null));
            }
        });
    }

    @Override
    public IPage<BusinessNft> getBusinessNftPage(PageParam<BusinessNft> page, BusinessNft businessNft) {
        return businessNftMapper.getBusinessNftPage(page, businessNft);
    }

    @Override
    public List<BusinessNft> getInfoList(Integer num) {
        return businessNftMapper.getInfoList(num);
    }
}
